package blue.blog.core.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import blue.blog.core.dao.CmsChannelDao;
import blue.blog.core.dao.CmsDocContentDao;
import blue.blog.core.dao.CmsDocDao;
import blue.blog.core.dao.CmsDocFileDao;
import blue.blog.core.model.CmsDoc;
import blue.blog.core.model.CmsDocFile;

@Service
@Transactional
public class CmsDocService
{
	@Resource
	private CmsDocDao cmsDocDao;
	@Resource
	private CmsDocContentDao cmsDocContentDao;
	@Resource
	private CmsDocFileDao cmsDocFileDao;
	@Resource
	private CmsChannelDao cmsChannelDao;
	
	public CmsDocService()
	{
	}
	
	public boolean exist(String url)
	{
		return cmsDocDao.exist(url);
	}
	
	public void add(CmsDoc doc)
	{
		if (cmsDocDao.exist(doc.getUrl()))
			return;
		
		cmsDocDao.save(doc);
		cmsDocContentDao.add(doc.getId(), doc.getSummary(), doc.getContent());
		List<CmsDocFile> fileList = doc.getFileList();
		if (fileList != null && !fileList.isEmpty())
		{
			for (CmsDocFile file : fileList)
			{
				file.setDocId(doc.getId());
			}
			cmsDocFileDao.saveList(fileList);
		}
		cmsChannelDao.incDocs(doc.getChannelId(), 1);
	}
	
}
